package yxy.game.pm2.bean.module.rank;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.script.fomula;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.PlayerModule;

import java.util.HashSet;

public class RankWorshipPlayer extends PlayerModule {

	// bean==========================================================================

	private String rankName;
	private DateTime updateTime;
	private HashSet<String> worshiped = new HashSet<>();

	public String getRankName() {
		return rankName;
	}

	public void setRankName(String rankName) {
		this.rankName = rankName;
	}

	public DateTime getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(DateTime updateTime) {
		this.updateTime = updateTime;
	}

	public HashSet<String> getWorshiped() {
		return worshiped;
	}

	public void setWorshiped(HashSet<String> worshiped) {
		this.worshiped = worshiped;
	}
	// func==========================================================================

	private void update() {
		try {
			if (tryLock()) {
				DateTime now = DateTime.now();
				if (getUpdateTime() == null || !new LocalDate(now).equals(new LocalDate(getUpdateTime()))) {
					setUpdateTime(now);
					worshiped.clear();
					this.set();
				}
			}
		} catch (Exception e) {
		} finally {
			unlock();
		}
	}

	public int remainTimes() {
		int maxtimes = fomula.排行榜每日膜拜次数.intValue();
		return maxtimes - worshiped.size();
	}

	// redis=========================================================================

	static public String key(String playerCode, String rankName) {
		return String.format("PLAYER:%s:MODULE:WORSHIP:%s", playerCode, rankName);
	}

	public void set() {
		try {
			setv(key(getPlayerCode(), getRankName()), this);
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	static public RankWorshipPlayer get(String playerCode, RankWorship worship) {
		try {
			RankWorshipPlayer module = getv(key(playerCode, worship.getRankName()));
			if (module == null) {
				module = MySQLCacheDAO.getV(key(playerCode,worship.getRankName()), RankWorshipPlayer.class);
				if(module != null){//同步到redis
					module.set();
				}
			}

			if (module == null) {
				module = create(playerCode, worship.getRankName());
			}
			module.update();
			return module;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	static private RankWorshipPlayer create(String playerCode, String rankName) {
		RankWorshipPlayer module = new RankWorshipPlayer();
		module.setPlayerCode(playerCode);
		module.setRankName(rankName);
		return module;
	}

	//存储到数据库的方法
	public static void saveToDB(String playerCode) {
		try {
			for(String rankName:PlayerRankModule.getAllRankNames()) {
				String key = key(playerCode, rankName);
				if (touchBucket(key).isExists()) {
					RankWorshipPlayer rankWorshipPlayer = getv(key(playerCode, rankName));
					MySQLCacheDAO.setV(key, rankWorshipPlayer);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//从redis移走数据的操作
	public static void removeAllFromRedis(String playerCode) {
		try {
			for(String rankName:PlayerRankModule.getAllRankNames()) {
				touchBucket(key(playerCode, rankName)).delete();
			}
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}
	// lock==========================================================================

	@Override
	public String lockKey() {
		return String.format("lock:%s", key(getPlayerCode(), getRankName()));
	}

}
